﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using System.Web.Mvc;
using ZSoftInstagram.Domain.Board;
using ZSoftInstagram.Services.Abstraction;
using ZSoftInstagram.UI.ViewModels;
using ZSoftInstagram.UI.Web.Infrastructure.AppEvents.BaseControllers;
using ZSoftInstagram.UI.Web.Infrastructure.Filters;

namespace ZSoftInstagram.UI.Web.Controllers
{
    public class ImagePinController : EntityController<ImagePin>
    {
        IPinService _service;

        protected override IEntityService<ImagePin> Service
        {
            get
            {
                return _service;
            }
        }


        [HttpGet]
        public async Task<ActionResult> Search(string q)
        {
            
            return View(await this._service.GetByTagAsync(q.Split(',')));
        }


        public ImagePinController(IPinService service)
        {
            this._service = service;
        }
        
        [HttpGet]
        public async Task<ActionResult> Repin([Required]int pinId)
        {
            var boards = await GetService<IPinBoardService>().GetUserBoardsAsync(this.CurrentUserId);
            var model = new Tuple<List<PinBoardDashBoardModel>, int>(boards, pinId);
            return View(model);
        }

        //[Authorize, AjaxOnly, ValidateModel, HttpPost]
        public async Task<ActionResult> Repin(RePinModel model)
        {
            return (await _service.RepinAsync(model, this.CurrentUserId)).ToDbServiceResult();
        }

        [ValidateModel, AjaxOnly, Authorize, HttpPost]
        public async Task<ActionResult> Like(AddLikeModel model)
        {
            return (await this._service.AddLikeAsync(model, this.CurrentUserId)).ToDbServiceResult();
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Create(int BoardId)
        {
            ViewBag.BoardId = BoardId;
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> Upload(PinCreateModel model)
        {
            return (await this._service.CreateAsync(model)).ToDbServiceResult();
        }

        [HttpPost, ValidateModel, AjaxOnly]
        public async Task<ActionResult> Delete(PinDeleteModel model)
        {
            return (await this._service.DeleteAsync(model)).ToDbServiceResult();
        }
    }
}